<?php
error_reporting(0);
date_default_timezone_set('PRC');
require_once 'mysql_config.php';
require_once 'mysql.php';
require_once 'file.php';

$my = new mysql($db);
if ($_GET['add']) {
	$hours = date('H');
	if ($hours < 9) {
		echo '不抓取';exit;
	}
	//获取列表名称
	$list = get_content('http://hotels.ctrip.com/Domestic/Tool/AjaxHotelList.aspx?cityId=91&star=5&page=1');
	$list = json_decode($list, true);
	$list = $list['hotelPositionJSON'];


	$arr = array(
		'994296' => '九寨沟天源豪生度假酒店',
		'430504' => '九寨沟喜来登国际大酒店',
		'5434068' => '九寨沟悦榕庄',
		'6902219' => '九寨鲁能希尔顿度假酒店',
		'430535' => '九寨天堂洲际大饭店',
	);

	foreach ($arr as $k => $v) {
		$data[] = get_data($k, $list);
	}

	// $db_host = 'localhost';
	// $db_user = 'root';
	// $db_pwd = '';
	// $db_database = 'jd';
	// $conn = '';
	// $coding = 'utf8';
	// $c = mysql_connect('47.92.66.212', 'root', 'long');
	// mysql_select_db('mysql', $c);
	// $mysql = new mysql($db_host, $db_user, $db_pwd, $db_database, $conn, $coding);
	foreach ($data as $k => $v) {
		$row['hid'] = $v['hid'];
		$row['name'] = $v['name'];
		$row['newTime'] = $v['newTime'];
		$row['price'] = $v['price'];
		$row['isUpdate'] = $v['isUpdate'];
		$row['updateTime'] = $v['updateTime'];
		// $res = $my->save('jd', $row);
		unset($row);
	}


	echo '成功';exit;
} else {
	$date = $_GET['date'] == '' ? date("Y-m-d") : $_GET['date'];

	$list = $my->getall("select * from jd where TO_DAYS(createTime) = TO_DAYS('{$date}') order by id asc");


	// $headerArr = array('抓取时间','酒店名称','最新预订');
	// $dataArr = array();
	// foreach ($list as $v) {
	// 	$time = date('YmdHi', strtotime($v['createTime']));
	// 	$data[$time][] = $v;
	// }

	// foreach ($data as $k => $v) {
	// 	foreach ($v as $k1 => $v1) {
	// 		$dataArr[] = array($v1['createTime'], $v1['name'], $v1['newTime']);
	// 	}
	// 	$dataArr[] = array('','','','');
	// }

	$headStr = iconv('utf-8','gb2312','抓取时间')."\t".iconv('utf-8','gb2312','酒店名称')."\t".iconv('utf-8','gb2312','最新预订')."\t".iconv('utf-8','gb2312','价格')."\t".iconv('utf-8','gb2312','是否更新')."\t".iconv('utf-8','gb2312','更新时间')."\r\n";
	// $dataStr = array();
	foreach ($list as $v) {
		$time = date('YmdHi', strtotime($v['createTime']));
		$data[$time][] = $v;
	}

	//更新个数
	$updateCountArr = array();
	foreach ($data as $k => $v) {
		foreach ($v as $k1 => $v1) {
			if ($v1['isUpdate'] > 0) {
				$updateCountArr[$v1['hid']]['id'] = $v1['hid'];
				$updateCountArr[$v1['hid']]['name'] = $v1['name'];
				$updateCountArr[$v1['hid']]['count']++;
			}
			$isUpdate = $v1['isUpdate'] > 0 ? '是' : '';
			$updateTime = $v1['isUpdate'] > 0 && strtotime($v1['updateTime']) > 0 ? $v1['updateTime'] : '';
			$headStr .= iconv('utf-8','gb2312',$v1['createTime'])."\t".iconv('utf-8','gb2312',$v1['name'])."\t".iconv('utf-8','gb2312',$v1['newTime'])."\t".iconv('utf-8','gb2312',$v1['price'])."\t".iconv('utf-8','gb2312',$isUpdate)."\t".iconv('utf-8','gb2312',$updateTime)."\r\n";
		}
		$headStr .= "\r\n";
		// $dataArr[] = array('','','','');
	}
	$headStr .= "\r\n\r\n";
	$headStr .= iconv('utf-8','gb2312','名称')."\t".iconv('utf-8','gb2312','更新数量')."\r\n";
	foreach ($updateCountArr as $v) {
		$headStr .= iconv('utf-8','gb2312',$v['name'])."\t".iconv('utf-8','gb2312',$v['count'])."\r\n";
	}


	$count = $my->getone("select count from tongji where TO_DAYS(createTime) = TO_DAYS('{$date}') order by id desc");
	$tongji_row['count'] = $count['count'] + 1;
	$tongji_row['remoteAddr'] = $_SERVER['REMOTE_ADDR'];
	$my->save('tongji', $tongji_row);

	File::export_excel('jd.xls', $headStr);

	// File::export_phpexcel('jd', $headerArr);
}





function get_content($url, $referer = '') {

    $cip = '123.125.68.'.mt_rand(0,254);
    $xip = '125.90.88.'.mt_rand(0,254);

    $referer = $referer == '' ? 'http://www.163.com' : $referer;

	$headers['CLIENT-IP'] = $cip; 
	$headers['X-FORWARDED-FOR'] = $xip;
	$headers['Host'] = 'hotels.ctrip.com';
	$headers['Referer'] = $referer;
	$headerArr = array(); 
	foreach( $headers as $n => $v ) { 
	    $headerArr[] = $n .':' . $v;  
	}
	$url1 = 'http://localhost/my.php';
	ob_start();
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_HTTPHEADER , $headerArr );  //构造IP
	// curl_setopt ($ch, CURLOPT_REFERER, "http://hotels.ctrip.com/hotel/430504.html");   //构造来路
	curl_setopt( $ch, CURLOPT_HEADER, 0);
	curl_exec($ch);
	curl_close ($ch);
	$out = ob_get_contents();
	ob_clean();
	return $out;
}

function get_data($id, $list) {
	$url = "http://hotels.ctrip.com/Domestic/tool/AjaxHote1RoomListForDetai1.aspx?hotel={$id}";
	$referer = "http://hotels.ctrip.com/hotel/{$id}.html";
	
	$res = get_content($url,$referer);
	$res = json_decode($res, true);
	unset($res['ASYS']);
	unset($res['html']);
	unset($res['ComplexHtml']);
	$name = '';
	foreach ($list as $v) {
		if ($v['id'] == $id) {
			$name = $v['name'];break;
		}
	}
	$isUpdate = 0;
	$updateTime = '';
	if (strpos($res['lastbookinfo'], '分钟') > 0) {
		global $my;
		$newTime = $res['lastbookinfo'];
		$newTime = str_replace('最新预订：', '', $newTime);
		$newTime = str_replace('分钟前', '', $newTime);
		$newTime = intval($newTime) * 60;
		$nowTime = strtotime(date('Y-m-d H:i'));
		$cha = $nowTime - $newTime;
		$info = $my->getone("select * from jd where hid = {$id} order by id desc");
		$updateTime = date('Y-m-d H:i', $nowTime - $newTime);
		if (strtotime($updateTime) > strtotime($info['updateTime'])) {
			$isUpdate = 1;
		}
	}
	$data['hid'] = $id;
	$data['name'] = $name;
	$data['newTime'] = $res['lastbookinfo'];
	$data['price'] = $res['PriceZl'];
	$data['isUpdate'] = $isUpdate;
	$data['updateTime'] = $updateTime;

	return $data;
}




?>